package com.qf.day14.ddz;

import java.util.*;

public class PukeCardUtils {

    // 声明扑克点数的数组
    public static final String[] POINTS_ARR = {"3","4","5","6","7","8","9","10","J","Q","K","A","2","SJOKER","BJOKER"};

    // 声明扑克花色的数组
    public static final String[] PUKE_TYPE = {"♥","♠","♦","♣"};


    /**
     * 模拟斗地主抓牌
     * @param cards  54张牌
     * @param p1  玩家1的手牌
     * @param p2
     * @param p3
     * @param d1  底牌，剩三张
     */
    public static void drawPukeCard1(List<PukeCard> cards, List<PukeCard> p1, List<PukeCard> p2,
                                     List<PukeCard> p3, List<PukeCard> d1){
        Collections.shuffle(cards);
        LinkedList<PukeCard> pukeCards = new LinkedList<>(cards);
        int size = pukeCards.size()-3;
        while (size>0) {
            p1.add(pukeCards.pop());
            p2.add(pukeCards.pop());
            p3.add(pukeCards.pop());
            size-=3;
        }
        d1.addAll(pukeCards);
    }

    public static void drawPukeCard(List<PukeCard> cards, List<PukeCard> p1, List<PukeCard> p2,
                                    List<PukeCard> p3, List<PukeCard> d1){
        Collections.shuffle(cards);
        int size = cards.size()-3;
        for (int i = 0; i < size; i++) {
            if(i%3 == 0){
                p1.add(cards.get(i));
            }
            if(i%3 == 1){
                p2.add(cards.get(i));
            }
            if(i%3 == 2){
                p3.add(cards.get(i));
            }
        }
        d1.addAll(cards.subList(size,cards.size()));
    }



    /**
     * 创建一副扑克牌
     * @return
     */
    public static List<PukeCard> createPukeList(){

        ArrayList<PukeCard> pukeCards = new ArrayList<>();

        for (int i = 0; i < POINTS_ARR.length-2; i++) {
            for (String type : PUKE_TYPE) {
                PukeCard pukeCard = new PukeCard(type, POINTS_ARR[i], i);
                pukeCards.add(pukeCard);
            }
        }
        // 单独处理大小王
        PukeCard sjoker = new PukeCard("", "SJOKER", 13);
        PukeCard bjoker = new PukeCard("", "BJOKER", 14);
        pukeCards.add(sjoker);
        pukeCards.add(bjoker);


        return pukeCards;
    }

    /**
     * 炸弹个数
     * @param p 手牌List数组
     * @return 返回炸弹数
     */
    public static Integer countBomb(List<PukeCard> p) {
        HashMap<Integer, Integer> hashMap = new HashMap<>(); //点数 次数
        ArrayList<Map.Entry<Integer, Integer>> arrayList = new ArrayList<>();
        for (PukeCard card : p) {
            hashMap.put(card.getValue(),hashMap.get(card.getValue()) == null ? 1: hashMap.get(card.getValue())+1);
        }
        Set<Map.Entry<Integer, Integer>> entries = hashMap.entrySet();
        Boolean flag1 = false;
        Boolean flag2 = false;
        for (Map.Entry<Integer, Integer> entry : entries) {
            if(entry.getValue() == 4){
                arrayList.add(entry);
            }
            if(entry.getKey()==13){
                flag1= true;
            }
            if(entry.getKey()==14){
                flag2 = true;
            }
        }
        int result = arrayList.size();
        if(flag1 && flag2){
            result+=1;
        }
        return result;
    }

    /**
     * 叫地主方法
     * @param p1
     * @param p2
     * @param p3
     * @param d1
     */
    public static void getCard(List<PukeCard> p1,List<PukeCard> p2,List<PukeCard> p3,List<PukeCard> d1) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("谁叫地主: 1.p1 2.p2 3.p3");
        String s = scanner.next();
        switch (s){
            case "1":
                p1.addAll(d1);
                break;
            case "2":
                p2.addAll(d1);
                break;
            case "3":
                p3.addAll(d1);
                break;
        }
    }
    public static void pukeSort(List<PukeCard> p1,List<PukeCard> p2,List<PukeCard> p3){
        p1.sort((o1, o2) -> o1.getValue()- o2.getValue());
        p2.sort((o1, o2) -> o1.getValue()- o2.getValue());
        p3.sort((o1, o2) -> o1.getValue()- o2.getValue());
    }
    public static void main(String[] args) {
        List<PukeCard> pukeList = createPukeList();
//        for (PukeCard pukeCard : pukeList) {
//            System.out.println(pukeCard);
//        }
//        System.out.println(pukeList.size());

        ArrayList<PukeCard> p1 = new ArrayList<>();
        ArrayList<PukeCard> p2 = new ArrayList<>();
        ArrayList<PukeCard> p3 = new ArrayList<>();
        ArrayList<PukeCard> d1 = new ArrayList<>();
        //发牌
        drawPukeCard(pukeList,p1,p2,p3,d1);

        pukeSort(p1,p2,p3);
        System.out.println(p1);
        System.out.println(p2);
        System.out.println(p3);

        //叫地主
        getCard(p1,p2,p3,d1);
        //排序
        pukeSort(p1,p2,p3);

        System.out.println(p1);
        System.out.println(p2);
        System.out.println(p3);
        System.out.println(d1);
        //统计炸弹
        System.out.println("p1炸弹个数："+countBomb(p1));
        System.out.println("p2炸弹个数："+countBomb(p2));
        System.out.println("p3炸弹个数："+countBomb(p3));

    }




}
